package com.yulongtian.algorithms.data_structure.level1;

/**
 * https://leetcode.cn/problems/maximum-subarray/
 *
 * @author yulongTian
 * @create 2023-03-01 9:26
 */
public class Test01 {
    public static void main(String[] args) {
        System.out.println(maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }

    //动态规划
    public static int maxSubArray(int[] nums) {
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
            max = Math.max(max, nums[i]);
        }
        return max;
    }

    //超时  203 / 210 个通过测试用例
//    public static int maxSubArray(int[] nums) {
//        int ans = Integer.MIN_VALUE;
//        int[] dp = new int[nums.length];
//        for (int i = 0; i < nums.length; i++) {
//            dp[i] = nums[i];
//            ans = Math.max(dp[i], ans);
//            for (int j = i + 1; j < nums.length; j++) {
//                dp[j] = dp[j - 1] + nums[j];
//                ans = Math.max(dp[j], ans);
//            }
//        }
//        return ans;
//    }

}
